package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.EvaluateCo;
import com.engineering.elink.service.EvaluateService;
import com.engineering.elink.vo.EvaluateAddVo;
import com.engineering.elink.xaservice.EvaluateXaService;

@RestController
@CrossOrigin
@RequestMapping("/api/skillevaluate")
@Api(value="技能评价接口")
@ApiIgnore
public class SkillEvaluateController {
	@Autowired
	private EvaluateService evaluateService;
	
	@Autowired
	private EvaluateXaService evaluateXaService;
	
	/**
	 * 发表评价
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "发表评价")
	public Map<String, Object> add(
			@ApiParam(value = "技能id", required = true) @RequestParam("id") Integer id,
			@ApiParam(value = "评价内容", required = true) @RequestParam("content") String content,
			@ApiParam(value = "评分数", required = true) @RequestParam("evalCount") Integer evalCount
			,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return evaluateXaService.add(new EvaluateAddVo(id,userId,content,evalCount,1));
	}
	
	/**
	 * 评价点赞(顶/踩)
	 */
	@RequestMapping(value = "addPraiseEvaluate", method = RequestMethod.POST)
	@ApiOperation(value = "评价点赞(顶/踩)")
	public Map<String, Object> addPraiseEvaluate(
			@ApiParam(value = "评价id", required = true) @RequestParam("commentId") Integer commentId,
			@ApiParam(value = "点赞类别，1-顶，2-踩", required = true,defaultValue="1") @RequestParam("praiseType") Integer praiseType,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return evaluateService.addPraiseEvaluate(commentId, praiseType, userId,2);
	}
	
	/**
	 * 
	 * 分页获取技能的一级评价
	 */
	@RequestMapping(value = "/getTopEvaluatesByCourseId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取技能的一级评价")
	public Map<String, Object> getTopEvaluatesByCourseId(@ModelAttribute @Valid EvaluateCo co) {
		//技能传1
		return evaluateService.getTopEvaluatesByCourseId(co,1);
	}
	
	/**
	 * 获取我评价的技能
	 */
	@RequestMapping(value="/getMyEvaluate",method=RequestMethod.GET)
	@ApiOperation(value="获取我评价的")
	public 	Map<String, Object> getMyEvaluate(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		//技能传1
		return evaluateService.getMyEvaluate(co, userId,1);
	}
	
	/**
	 * 获取评价我的技能
	 */
	@RequestMapping(value="/getEvaluateForMe",method=RequestMethod.GET)
	@ApiOperation(value="获取评价我的")
	public Map<String, Object> getEvaluateForMe(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		//技能传1
		return evaluateService.getEvaluateForMe(co, userId,1);
		
	}
	
	/**
	 * 分页获取一级评价下的所有回复评价
	 */
	@RequestMapping(value = "/getAllEvaluatesByTopId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取一级评价下的所有回复评价")
	public Map<String, Object> getAllEvaluatesByTopId(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="一级评价的id",required=true) @RequestParam("topId")Integer topId) {
		
		//技能传1
		return evaluateService.getAllEvaluatesByTopId(co,topId,1,true);
	}
	
	@RequestMapping(value = "/getAllByTopId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取一级评价下的所有回复评价包括当前评价")
	public Map<String, Object> getAllByTopId(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="一级评价的id",required=true) @RequestParam("topId")Integer topId) {
		
		//技能传1
		return evaluateService.getAllEvaluatesByTopId(co,topId,1,false);
	}
	
	/**
	 * 回复评价
	 */
	@RequestMapping(value = "/addReplyEvaluate", method = RequestMethod.POST)
	@ApiOperation(value = "回复评价")
	public Map<String, Object> addReplyEvaluate(
			@ApiParam(value = "技能id", required = true) @RequestParam("id") Integer id,
			@ApiParam(value = "评价id", required = true) @RequestParam("commentId") int commentId,
			@ApiParam(value = "评价内容", required = true) @RequestParam("content") String content,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		//技能传1
		return evaluateXaService.addReplyEvaluate(id, commentId, userId, content,1);
	}
	
}
